package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.ExecutePurchaseDto;
import com.xt.purchasingsystem.dto.OutboundProcurementDto;
import com.xt.purchasingsystem.mapper.BuyerPlanDetailMapper;
import com.xt.purchasingsystem.mapper.BuyerPlanMapper;
import com.xt.purchasingsystem.mapper.OutboundProcurementDetailMapper;
import com.xt.purchasingsystem.mapper.OutboundProcurementMapper;
import com.xt.purchasingsystem.model.BuyerPlanDetail;
import com.xt.purchasingsystem.model.OutboundProcurement;
import com.xt.purchasingsystem.model.OutboundProcurementDetail;
import com.xt.purchasingsystem.service.OutboundProcurementService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-06-06
 */
@Service
public class OutboundProcurementServiceImpl extends ServiceImpl<OutboundProcurementMapper, OutboundProcurement> implements OutboundProcurementService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private OutboundProcurementMapper outboundProcurementMapper;
    @Autowired
    private OutboundProcurementDetailMapper outboundProcurementDetailMapper;
    @Autowired
    private BuyerPlanMapper buyerPlanMapper;
    @Autowired
    private BuyerPlanDetailMapper buyerPlanDetailMapper;

    @Override
    @Transactional
    public Result saveOutboundProcurement(OutboundProcurementDto outboundProcurementDto) {
        String id = String.valueOf(idWorker.nextId());
        outboundProcurementDto.getOutboundProcurement().setId(id);
        outboundProcurementMapper.insert(outboundProcurementDto.getOutboundProcurement());
        for (OutboundProcurementDetail outboundProcurementDetail : outboundProcurementDto.getOutboundProcurementDetails()) {
            outboundProcurementDetail.setId(String.valueOf(idWorker.nextId()));
            outboundProcurementDetail.setOutboundProcurementId(id);
            outboundProcurementDetailMapper.insert(outboundProcurementDetail);
        }
        return ResultFactory.buildSuccessResult("添加成功");
    }

    @Override
    public Result getAllNoPurchaseByPage(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);
        List<OutboundProcurementDto> allNoPurchaseByPage = outboundProcurementMapper.getAllNoPurchaseByPage();
        PageInfo<OutboundProcurementDto> pageInfo = new PageInfo<>(allNoPurchaseByPage);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }

    @Override
    @Transactional
    public Result updateOutboundProcurementAndDetailsPurchaseAmount(OutboundProcurementDto outboundProcurementDto) {
        outboundProcurementMapper.updateById(outboundProcurementDto.getOutboundProcurement());
        for (OutboundProcurementDetail outboundProcurementDetail : outboundProcurementDto.getOutboundProcurementDetails()) {
            outboundProcurementDetailMapper.updateById(outboundProcurementDetail);
        }
        return ResultFactory.buildSuccessResult("修改成功");
    }

    @Override
    @Transactional
    public Result executePurchase(ExecutePurchaseDto executePurchaseDto) {
        String id = String.valueOf(idWorker.nextId());
        executePurchaseDto.getBuyerPlanDto().getBuyerPlan().setId(id);
        CompletableFuture.runAsync(() -> {
            buyerPlanMapper.insert(executePurchaseDto.getBuyerPlanDto().getBuyerPlan());
        }, threadPoolExecutor);
        CompletableFuture.runAsync(() -> {
            for (BuyerPlanDetail buyerPlanDetail : executePurchaseDto.getBuyerPlanDto().getBuyerPlanDetailList()) {
                buyerPlanDetail.setId(String.valueOf(idWorker.nextId()));
                if (buyerPlanDetail.getGather().indexOf(",") != -1) {
                    buyerPlanDetail.setGather(buyerPlanDetail.getGather().substring(0, buyerPlanDetail.getGather().length() - 1));
                }
                buyerPlanDetail.setBuyerPlanId(id);
                buyerPlanDetailMapper.insert(buyerPlanDetail);
            }
        }, threadPoolExecutor);

        CompletableFuture.runAsync(() -> {
            for (OutboundProcurementDto outboundProcurementDto : executePurchaseDto.getOutboundProcurementDtos()) {
                OutboundProcurement outboundProcurement = outboundProcurementDto.getOutboundProcurement();
                outboundProcurement.setPurchaseTag("P001-1");
                outboundProcurementMapper.updateById(outboundProcurement);
            }
        }, threadPoolExecutor);

        return ResultFactory.buildSuccessResult("添加成功,需要审核!");
    }
}
